﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.Xml.XmlDocument>" %>

<%= Documents.Common.Helpers.XmlHelper.TransformXML(Model,Server.MapPath(@"~\Transforms\Document.xslt")) %>



<script type="text/javascript">

   
    // We only want to load the history if this document been selected from the document list
    // if it's been selected from the history list then we already have the history loaded and so no
    // need to re-load it.
    
    var documentorigin = $("#state #documentOrigin").val();
    var selectedCategory = $("#selectedCategory").val();
    if (documentorigin != "history") {

        var url = "/Home/History/" + $("#documentsetid").val();
        var ajaxOptions = {
            url: url,
            cache: false,
            type: "POST",
            success: LoadDocumentHistorySuccess,
            error: LoadDocumentHistoryFailure
        };
        $.ajax(ajaxOptions);
    };


    $("#showdocumentbutton").click(function() {
        if (!$(this).hasClass("disabled")) {
            eventHub.RaiseEvent(DocumentEvents.OnDocumentView);
        };
    });

    $("#showhistorybutton").click(function() {
        if (!$(this).hasClass("disabled")) {
            eventHub.RaiseEvent(DocumentEvents.OnDocumentHistorySelected);
        };
    });

    $("#maximizedocumentbutton").click(function() {
        if (!$(this).hasClass("disabled")) {
            eventHub.RaiseEvent(DocumentEvents.OnDocumentMaximize);
        };
    });

    $("#restoredocumentbutton").click(function() {
        if (!$(this).hasClass("disabled")) {
            eventHub.RaiseEvent(DocumentEvents.OnDocumentRestore);
        };
    });


    $("#revokebutton").click(function() {
        if (!$(this).hasClass("disabled")) {
            var url = "/Home/Revoke/" + $(this).attr("documentid");
            var ajaxOptions = {
                url: url,
                cache: false,
                type: "POST",
                success: RevokeSuccess,
                error: RevokeFailure
            };
            $.ajax(ajaxOptions);
        }
    });


    $("#showmetadatabutton").click(function() {
        if (!$(this).hasClass("disabled")) {
            $.blockUI({
                message: $("#metadatamodal"),
                css: { border: '0px solid #a00' },
                overlayCSS: { backgroundColor: '#eee', opacity: 0.5 }
            });
        };
    });


    $("#btnCloseMetaDataModal").click(function() {
        $.unblockUI();
    });
    
    function RevokeSuccess(data) {
        // Update the document contents
        $("#documentcontainer").html(data);
        // Update the Document list to contain the new document version at the current position
        // $("#documents tbody tr.selected").attr("DocumentID",documentid from data above);
        // if (document from data above is revoked) then $("#documents tbody tr.selected .revokestatus").val(true) or something.....
    };

    function RevokeFailure(xhr, description, error) {
        // log it, or do something...
        alert("Nao");
    };
    
    function LoadDocumentHistorySuccess(data) {
        // Update the history
        $("#historycontainer").html(data);
        eventHub.RaiseEvent(DocumentEvents.OnDocumentHistoryLoaded);
    };

    function LoadDocumentHistoryFailure(xhr, description, error) {
        // log it, or do something...
        alert("Nao");
    };
</script>